package org.leo.dm.example.plugin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.Element;
import org.leo.dm.common.bean.Constants;
import org.leo.dm.common.exception.DpException;
import org.leo.dm.common.exception.DpExceptionCodeEnum;
import org.leo.dm.common.exception.DpExceptionTracker;
import org.leo.dm.component.plugin.queueplugin.QueuePlugin;
import org.leo.dm.example.base.GlobalVariables;
import org.leo.dm.util.ToUtil;
import org.leo.dm.util.comm.Dom4jUtils;

public class PluginQueueCustom extends QueuePlugin {

	@Override
	public String handle(String name) throws Exception {

		GlobalVariables.rangeCodePlfcodeMap = new HashMap<String, Map<String, String>>();
		Map<String, String> plfcodeMap = new HashMap<String, String>();

		try {

			Document allRangeDocument = Dom4jUtils.parseFIOToXml("/com/neusoft/dp/dt/example/testxml/rangechange_2.xml");
			Element root = allRangeDocument.getRootElement();

			Element range = null;
			List<?> items = null;
			Element item = null;

			List<?> ranges = root.elements("range");
			int rangesSize = ranges.size();
			for (int rangeIndex = 0; rangeIndex < rangesSize; rangeIndex++) {
				range = (Element) ranges.get(rangeIndex);

				plfcodeMap = new HashMap<String, String>();
				GlobalVariables.rangeCodePlfcodeMap.put(range.element("rangecode").getText() + Constants.RANGE_VC_SEPARATOR + range.element("rangeversion").getText(), plfcodeMap);

				items = range.element("items").elements("item");
				int itemsSize = items.size();
				for (int itemIndex = 0; itemIndex < itemsSize; itemIndex++) {
					item = (Element) items.get(itemIndex);
					plfcodeMap.put(item.element("code").getText(), item.element("plfcode").getText());
				}
			}
			ToUtil.to("初始化值域映射完成");
		} catch (Exception e) {
			DpExceptionTracker.trace(e);
			throw new DpException(DpExceptionCodeEnum.PLUGIN_ERROR, "初始化值域映射过程发生异常");
		}

		GlobalVariables.setElementRangeMap = new HashMap<String, Map<String, String>>();
		Map<String, String> elementRangeMap = new HashMap<String, String>();

		try {

			Document allRangeDocument = Dom4jUtils.parseFIOToXml("/com/neusoft/dp/dt/example/testxml/rangechange_1.xml");
			Element root = allRangeDocument.getRootElement();

			Element set = null;
			List<?> elements = null;
			Element element = null;

			List<?> sets = root.elements("sets");
			int setsSize = sets.size();
			for (int setsIndex = 0; setsIndex < setsSize; setsIndex++) {
				set = (Element) sets.get(setsIndex);

				elementRangeMap = new HashMap<String, String>();
				GlobalVariables.setElementRangeMap.put(set.element("setcode").getText(), elementRangeMap);

				elements = set.elements("elements");
				int elementsSize = elements.size();
				for (int elementsIndex = 0; elementsIndex < elementsSize; elementsIndex++) {
					element = (Element) elements.get(elementsIndex);
					elementRangeMap.put(element.element("elementcode").getText(), element.element("rangecode").getText() + Constants.RANGE_VC_SEPARATOR + element.element("rangeversion").getText());
				}
			}
			ToUtil.to("初始化当前交换标准值域映射完成");
		} catch (Exception e) {
			DpExceptionTracker.trace(e);
			throw new DpException(DpExceptionCodeEnum.PLUGIN_ERROR, "初始化当前交换标准值值域映射过程发生异常");
		}
		return null;
	}

}
